import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox
from PyQt5.QtCore import QTimer, Qt


class CountdownApp(QWidget):
    def __init__(self):
        super().__init__()
        self.running = False
        self.pause_state = False
        self.total_seconds = 0
        self.initUI()
        # 窗口显示前，将其移到屏幕中心
        self.center_window()

    def initUI(self):
        self.setWindowTitle('倒计时程序')
        self.setGeometry(0, 0, 200, 200)  # 先设置初始大小（后续会调整位置）

        self.minute_label = QLabel('分钟:')
        self.minute_label.setStyleSheet('font-size: 16px;')
        self.minute_entry = QLineEdit()
        self.minute_entry.setFixedWidth(100)
        self.minute_entry.setStyleSheet('font-size: 16px;')

        self.second_label = QLabel('秒:')
        self.second_label.setStyleSheet('font-size: 16px;')
        self.second_entry = QLineEdit()
        self.second_entry.setFixedWidth(100)
        self.second_entry.setStyleSheet('font-size: 16px;')

        self.start_button = QPushButton('开始倒计时')
        self.start_button.clicked.connect(self.start_countdown)
        self.start_button.setStyleSheet('font-size: 16px; padding: 10px;')

        self.stop_button = QPushButton('停止')
        self.stop_button.clicked.connect(self.stop_countdown)
        self.stop_button.setDisabled(True)
        self.stop_button.setStyleSheet('font-size: 16px; padding: 10px;')

        self.pause_button = QPushButton('暂停')
        self.pause_button.clicked.connect(self.pause_countdown)
        self.pause_button.setDisabled(True)
        self.pause_button.setStyleSheet('font-size: 16px; padding: 10px;')

        self.timer_label = QLabel('00:00')
        self.timer_label.setStyleSheet('font-size: 36px;')
        self.timer_label.setAlignment(Qt.AlignCenter)

        input_layout = QHBoxLayout()
        input_layout.addWidget(self.minute_label)
        input_layout.addWidget(self.minute_entry)
        input_layout.addWidget(self.second_label)
        input_layout.addWidget(self.second_entry)
        input_layout.setSpacing(20)
        input_layout.setContentsMargins(0, 0, 0, 20)

        button_layout = QHBoxLayout()
        button_layout.addWidget(self.start_button)
        button_layout.addWidget(self.stop_button)
        button_layout.addWidget(self.pause_button)
        button_layout.setSpacing(20)

        main_layout = QVBoxLayout()
        main_layout.addLayout(input_layout)
        main_layout.addLayout(button_layout)
        main_layout.addWidget(self.timer_label)
        main_layout.setAlignment(Qt.AlignCenter)
        main_layout.setContentsMargins(50, 50, 50, 50)

        self.setLayout(main_layout)

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_countdown)

    def center_window(self):
        # 获取屏幕几何信息
        screen = QApplication.primaryScreen().geometry()
        # 获取窗口自身的几何信息
        window = self.geometry()
        # 计算窗口左上角的坐标（屏幕宽高 - 窗口宽高，再除以2）
        x = (screen.width() - window.width()) // 2
        y = (screen.height() - window.height()) // 2
        # 设置窗口位置
        self.move(x, y)

    def start_countdown(self):
        try:
            minutes = int(self.minute_entry.text())
            seconds = int(self.second_entry.text())
            self.total_seconds = minutes * 60 + seconds
            if self.total_seconds <= 0:
                raise ValueError
            self.running = True
            self.pause_state = False
            self.timer.start(1000)
            self.start_button.setDisabled(True)
            self.stop_button.setDisabled(False)
            self.pause_button.setDisabled(False)
        except ValueError:
            QMessageBox.critical(self, '错误', '请输入有效的分钟和秒数')

    def update_countdown(self):
        if self.running and not self.pause_state and self.total_seconds >= 0:
            minutes, seconds = divmod(self.total_seconds, 60)
            self.timer_label.setText(f'{minutes:02d}:{seconds:02d}')
            self.total_seconds -= 1
        elif self.total_seconds < 0:
            self.timer.stop()
            self.running = False
            self.start_button.setDisabled(False)
            self.stop_button.setDisabled(True)
            self.pause_button.setDisabled(True)
            QMessageBox.information(self, '提醒', '倒计时结束！')

    def stop_countdown(self):
        self.timer.stop()
        self.running = False
        self.start_button.setDisabled(False)
        self.stop_button.setDisabled(True)
        self.pause_button.setDisabled(True)

    def pause_countdown(self):
        self.pause_state = not self.pause_state
        if self.pause_state:
            self.pause_button.setText('继续')
        else:
            self.pause_button.setText('暂停')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = CountdownApp()
    ex.show()
    sys.exit(app.exec_())